<template>
    <el-dialog
        title="变更代理商"
        :visible.sync="showValue"
        append-to-body
        :close-on-click-modal="false"
        width="550px"
        top="20px"
        :before-close="clearData"
    >
        <div class="wrap">
            <el-form
                ref="form"
                :model="form"
                :rules="rules"
                size="small"
                label-width="120px"
            >
                <el-form-item label="代理商：" prop="agentId">
                    <el-select
                        class="items"
                        v-model="form.agentId"
                        filterable
                        clearable
                        placeholder="请选择"
                        size="small"
                    >
                        <el-option
                            v-for="item in agentList"
                            :key="item.id"
                            :label="item.agentName"
                            :value="item.id"
                        ></el-option>
                    </el-select>
                </el-form-item>
            </el-form>
        </div>
        <span slot="footer">
            <el-button size="small" @click="clearData">取 消</el-button>
            <el-button
                type="primary"
                size="small"
                :loading="loading"
                @click="save"
                >确 定</el-button
            >
        </span>
    </el-dialog>
</template>

<script>
export default {
    props: {
        value: Boolean,
        currRow: Object
    },
    data () {
        return {
            loading: false,
            showValue: false,
            agentList: [],
            form: {
                agentId: ''
            },
            rules: {
                agentId: [
                    { required: true, message: '不能为空', trigger: 'blur' }
                ]
            }
        }
    },
    computed: {
        roleId () {
            return this.$store.state.roleId
        }
    },
    watch: {
        value (val) {
            this.showValue = val
        },
        showValue (val) {
            this.$emit('update:value', val)
        }
    },
    created () {
        this.getAllAgent()
    },
    methods: {
        async getAllAgent () {
            let res = await this.$http.get(this.$store.state.IP + '/systemUserManage/getAgents', {
                data: {}
            })
            let response = res.data
            if (response.ret === '0') {
                console.log(res)
                this.agentList = response.res
            }
        },
        // 清空数据
        clearData () {
            this.$refs.form.resetFields()
            this.loading = false
            this.showValue = false
        },
        // 保存表单
        save () {
            this.$refs.form.validate(async (valid) => {
                if (valid) {
                    this.loading = true
                    let res = await this.$http.post(this.$store.state.IP + '/agent/terminalTransferOut', {
                        ...this.form,
                        chipId: this.currRow.chipID
                    })
                    let response = res.data
                    if (response.ret === '0') {
                        this.$message({
                            message: '保存成功',
                            type: 'success'
                        })
                        this.clearData()
                        this.$emit('getList')
                    } else {
                        this.loading = false
                    }
                }
            })
        }
    }
}
</script>

<style lang="stylus" rel="stylesheet/stylus" scoped>
.items
    width: 220px

.checkboxWrap
    height: 300px
    overflow: auto

.checkItem
    width: 180px
    display: flex
    align-items: center
    margin-left: 0 !important
</style>